package com.sibu.yx.promotion.admin.web.support.filter;

import com.doubo.json.util.JsonUtil;
import com.doubo.security.filter.authc.AuthenticationFilter;
import com.doubo.security.subject.Subject;
import com.doubo.security.util.ThreadContext;
import com.sibu.yx.promotion.admin.web.util.HttpUtil;
import com.sibu.yx.promotion.common.enums.ErrorCodeEnum;
import com.sibu.yx.promotion.common.manage.ResponseManage;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: qiujingwang
 * Date: 2016/4/19
 * Description: authc 过滤器（已登录）
 */
@Slf4j
@Deprecated
public class AuthcFilter extends AuthenticationFilter {

    private static String sessionTimeJson;

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        if (sessionTimeJson == null) {
            sessionTimeJson = JsonUtil.toJson(ResponseManage.fail(ErrorCodeEnum.SESSION_TIMEOUT));
        }
        //请重新登录
        HttpUtil.writeJsonDate(response, sessionTimeJson);
        return false;
    }

    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        return super.isAccessAllowed(request, response, mappedValue);
    }

    @Override
    protected Subject getSubject(HttpServletRequest request, HttpServletResponse response) {
        Subject subject = ThreadContext.getSubject();
        return subject;
    }

}
